package com.chuangke.sinotrukbudget.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chuangke.admin.model.SysDept;
import com.chuangke.admin.service.SysDeptService;
import com.chuangke.core.exception.ChuangkeException;
import com.chuangke.sinotrukbudget.model.BudgetAccount;
import com.chuangke.sinotrukbudget.model.BudgetBpcPlan;
import com.chuangke.sinotrukbudget.sdao.BudgetBpcPlanDaoService;
import com.chuangke.sinotrukbudget.service.BudgetQueryService;
import com.chuangke.sinotrukbudget.util.BudgetUtil;

@Service
public class BudgetQueryServiceImpl implements BudgetQueryService {
	
	@Autowired
	private SysDeptService sysDeptService;
	
	@Autowired
	private BudgetBpcPlanDaoService budgetBpcPlanDaoService ;

	@Override
	public List<Map<String, Object>> queryDeptReportedOverview(String category,String time, String deptfilter,List<String> deptIdList) {
		if(StrUtil.isBlank(category)) {
			throw new ChuangkeException("选择正确的预算版本") ;
		}
		
		List<SysDept> detailDeptIdList = sysDeptService.findMap().values().stream().filter(dept->"1".equals(dept.getIsLastLevel()) && (dept.getName().contains(deptfilter) || dept.getId().contains(deptfilter)))
				.collect(Collectors.toList()) ;
		
		QueryWrapper<BudgetBpcPlan> qw = new QueryWrapper<>();
		qw.select("t_entity,status,count(distinct t_entity,t_account) count").groupBy("t_entity,status") ;
		qw.lambda().eq(BudgetBpcPlan::gettCategory, category).eq(BudgetBpcPlan::getAccountType, BudgetAccount.ACCOUNT_TYPE_CW) ;
		if(!CollectionUtils.isEmpty(deptIdList)) {
			qw.lambda().in(BudgetBpcPlan::gettEntity, deptIdList) ;
		}
		
		List<String> timeList = BudgetUtil.getTimeList(category, time);
		
		qw.lambda().in(BudgetBpcPlan::gettTime, timeList);
		List<Map<String, Object>> list = budgetBpcPlanDaoService.listMaps(qw) ;
		
		if(!CollectionUtils.isEmpty(deptIdList)) {
			detailDeptIdList = detailDeptIdList.stream().filter(dept->deptIdList.contains(dept.getId())).collect(Collectors.toList()) ;
		}
		
		List<Map<String, Object>> result = new ArrayList<>() ;
		
		detailDeptIdList.forEach(dept->{
			Map<String, Object> data = new HashMap<>() ;
			List<Map<String, Object>> deptDatas = findByDept(list,dept.getId()) ;
			data.put("dept_id", dept.getId());
			data.put("dept_name", dept.getName()) ;
			if(CollectionUtils.isEmpty(deptDatas)) {
				data.put("is_reported", false) ;
			}else {
				data.put("is_reported", true) ;
				deptDatas.forEach(deptData-> data.put("status_"+deptData.get("status"), deptData.get("count")));
			}
			result.add(data) ;
		});
		
		return result ;
	}
	
	private List<Map<String, Object>> findByDept(List<Map<String, Object>> datas,String deptId){
		return datas.stream().filter(data->deptId.endsWith((String)data.get("t_entity"))).collect(Collectors.toList()) ;
	}
}
